#include "GroupTreeItem.h"
#include <algorithm>
#include "../StdUserInterfaceModule/Icons.h"

namespace Impp {


GroupTreeItem::GroupTreeItem(Group* group):
	group(group), extended(true)
{
	if(group!=NULL && group->hasFlag(GROUP_COLLAPSED)){
		this->extended = false;
	}
}

GroupTreeItem::~GroupTreeItem()
{
	for (size_t i = 0; i < children.size(); ++i) {
		if(children[i]){
			delete children[i];
		}
	}
}

void GroupTreeItem::paint(wxGCDC & dc, const wxPoint& pos, const wxSize& size, BuddyPriority priority, size_t hiddenCount, BuddyTreeList* list){

	wxBitmap icon = list->getExpandedIcon(extended);
	dc.DrawBitmap(icon,pos+wxPoint(0,1),true);

	wxString text = group->getName();
	text << wxT(" (") << group->getOnlineSize() << wxT("/") << group->getSize() << wxT(")");
	dc.DrawText(text, pos+wxPoint(18,1));


	if(priority != BUDDY_PRIORITY_LOW){
		wxSize textSize = dc.GetTextExtent(text);

		wxFont font = dc.GetFont();
		font.SetWeight(wxFONTWEIGHT_NORMAL);
		dc.SetFont(font);

		dc.SetTextForeground(wxColor(80,80,80,255));
		wxString hiddenText = wxString::Format(wxT(" %i hidden"), hiddenCount);
		dc.DrawText(hiddenText, pos+wxPoint(18 + textSize.x,1));
	}
}


void GroupTreeItem::setExtended(bool extended){
	if(group != NULL){
		if(!extended){
			group->setFlag(GROUP_COLLAPSED);
		}else{
			group->clearFlag(GROUP_COLLAPSED);
		}
	}
	this->extended=extended;
}

bool GroupTreeItem::isExtended(){
	return extended;
}

void GroupTreeItem::addChild(TreeItem* item){
	wxASSERT(item != NULL);
	children.push_back(item);
}

void GroupTreeItem::removeChild(TreeItem* item){
	wxASSERT(item != NULL);
	std::vector<TreeItem*>::iterator dIter(children.begin());

	for (; dIter != children.end(); ++dIter) {
	      if(*dIter==item){
	    	  children.erase(dIter);
	    	  return;
	      }
	}
}

BuddyTreeItem* GroupTreeItem::getChild(Buddy* buddy){
	wxASSERT(buddy != NULL);
	for (size_t i = 0; i < children.size(); ++i) {
		if(BuddyTreeItem* b = dynamic_cast<BuddyTreeItem*>(children[i])){
			if(b->getBuddy()==buddy){
				return b;
			}
		}
	}
	return NULL;
}

std::vector<TreeItem*> GroupTreeItem::getChildren(){
	return children;
}

Group* GroupTreeItem::getGroup(){
	return group;
}

void GroupTreeItem::sort(){
	std::sort(children.begin(),children.end(),GroupTreeItem::cmp);
}

bool GroupTreeItem::cmp(TreeItem* a, TreeItem* b){
	if(GroupTreeItem* ga = dynamic_cast<GroupTreeItem*>(a)){
		if(GroupTreeItem* gb = dynamic_cast<GroupTreeItem*>(b)){
			return ga->getGroup()->getName().Lower() < gb->getGroup()->getName().Lower();
		}else{
			return true;
		}
	}else if(BuddyTreeItem* ba = dynamic_cast<BuddyTreeItem*>(a)){
		BuddyTreeItem* bb = dynamic_cast<BuddyTreeItem*>(b);
		if( bb){
			const Status& sa = ba->getBuddy()->getStatus();
			const Status& sb = bb->getBuddy()->getStatus();

			if(sa == sb){
				return ba->getBuddy()->getName().Lower() < bb->getBuddy()->getName().Lower();
			}else{
				return sa < sb;
			}
		}else{
			return false;
		}

	}
	return true;
}

size_t GroupTreeItem::getSize(){
	return children.size();
}

}
